package cn.com.wxd.common.framedata.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.common.framedata.entity.FrameDataInfo;

/**
 * Title:
 * Description:
 * <p>
 * Company: wxd网站系统
 * Copyright: Copyright (c) 2017
 * All right reserved.
 * Created by WangXuDong on 2017/8/16.
 *
 * @author WangXuDong
 * @version 1.0
 */
public class Persistence {
    private static final Logger log = LoggerFactory.getLogger(Persistence.class);

    private Connection conn;

    public Persistence(Connection _conn){
        conn = _conn;
    }

    /**
     * 根据名称查询数据
     *
     * @param name
     *
     * @return
     */
    public FrameDataInfo selectRecordByName(String name) {
        FrameDataInfo frameDataInfo = null;
        if (name == null || name.isEmpty()) {
            log.warn("从FRAME_DATA数据库中查询参数 name 不能为NULL");
            return frameDataInfo;
        }
        String sql = "SELECT id,name,updatetime,data FROM FRAME_DATA WHERE name = ?";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                frameDataInfo =  new FrameDataInfo();
                frameDataInfo.setId(rs.getInt("id"));
                frameDataInfo.setName(rs.getString("name"));
                frameDataInfo.setData(rs.getString("updatetime"));
                frameDataInfo.setData(rs.getString("data"));
            }
            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            log.error("数据查询出现错误" , e);
        }
        return frameDataInfo;
    }

    /**
     * 添加数据
     *
     * @param fdi
     *
     * @return
     */
    public boolean addRecord(FrameDataInfo fdi) {
        int i = 0;
        String sql = "INSERT INTO FRAME_DATA (name,updatetime,data) values(?,?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fdi.getName());
            pstmt.setString(2, fdi.getUpdateTime());
            pstmt.setString(3, fdi.getData());
            i = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            log.error("更新FRAME_DATA数据出现错误,数据："+ fdi.toString() + "，堆栈：" , e);
        }
        return i == 1;
    }

    /**
     * 更新数据
     *
     * @param fdi
     *
     * @return
     */
    public boolean updateRecord(FrameDataInfo fdi) {
        if(fdi.getName() == null || fdi.getName().isEmpty()){
            log.warn("无法更新数据，因为更新FRAME_DATA表必须指定name值");
            return false;
        }
        int i = 0;
        String sql = "UPDATE FRAME_DATA SET updatetime = ? , data = ? where name = ?";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, fdi.getUpdateTime());
            pstmt.setString(2, fdi.getData());
            pstmt.setString(3, fdi.getName());
            i = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            log.error("更新FRAME_DATA数据出现错误,数据："+ fdi.getName() + "，堆栈：" , e);
        }
        return i == 1;
    }
}
